package com.example.demo.service.impl;

import com.example.demo.entity.Follow;
import com.example.demo.entity.Question;
import com.example.demo.exception.ServiceException;
import com.example.demo.mapper.IFollowMapper;
import com.example.demo.param.vo.FollowUserVO;
import com.example.demo.service.IFollowService;
import com.example.demo.util.Judge;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class FollowServiceImpl implements IFollowService {

    @Autowired private IFollowMapper followMapper;

    @Override
    public FollowUserVO getMyFollow(String uid, Integer page, Integer pageSize) throws ServiceException {
        if(page == null || page < 1 || pageSize == null || pageSize < 1){
            throw new ServiceException("请提供正确的分页");
        }
        FollowUserVO vo = this.followMapper.getMyFollow(uid, page, pageSize);
        vo.setPage(page);
        vo.setPageSize(pageSize);
        return vo;
    }

    @Override
    public FollowUserVO getMyFans(String uid, Integer page, Integer pageSize) throws ServiceException {
        if(page == null || page < 1 || pageSize == null || pageSize < 1){
            throw new ServiceException("请提供正确的分页");
        }
        FollowUserVO vo = this.followMapper.getMyFans(uid, page, pageSize);
        vo.setPage(page);
        vo.setPageSize(pageSize);
        return vo;
    }

    @Override
    public Boolean insert(String userId , String beUserId) throws ServiceException{
        if (Judge.isNull(userId) || Judge.isNull(beUserId)){
            throw new ServiceException("请提供正确的用户id");
        }
        Integer affect = followMapper.insert(Judge.getUUID(),userId,beUserId,0,new Date());
        if (affect != 1){
            throw new ServiceException("关注失败！");
        }
        return Boolean.TRUE;
    }

    @Override
    public Boolean delete(String id) throws ServiceException{
        if (Judge.isNull(id)){
            throw new ServiceException("请提供正确的用户id");
        }
        Integer affect = followMapper.delete(Judge.getUUID());
        if (affect != 1){
            throw new ServiceException("取消关注失败！");
        }
        return Boolean.TRUE;
    }

    /**
     * 测试
     * @param id
     * @return
     * @throws ServiceException
     */
    @Override
    public Follow getById(String id) throws ServiceException {
            if(Judge.isNull(id)){
                throw new ServiceException("请提供有效id");
            }
            Follow follow = followMapper.getById(id);
            return follow;
    }
}
